package  
{
	import org.flixel.FlxPoint;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxG;
	import org.flixel.FlxText;
	
	/**
	 * ...
	 * @author Jay Wagner
	 */
	public class PowerGauge extends FlxGroup 
	{
		public static const MINVOLTS:Number = 0;
		public static const THRESHOLD:int = 0;
		
		public static var maxVolts:int;
		public static var currentVolts:Number = 10;
		
		private var _gaugeVolts:Array;
		private var _powerText:FlxText;
		
		public function PowerGauge(X:int, Y:int, maxV:int = 10) 
		{
			super();
			
			var xOffset:int = 0;
			var textOffset:int = 95;
			
			scrollFactor.x = scrollFactor.y = 0;
			maxVolts = maxV;
			
			var bg:FlxSprite = new FlxSprite(X - textOffset - 35, Y);
			add(bg.createGraphic(365, 30, 0xCC000000, true), true);
			
			_gaugeVolts = new Array();
			
			for (var i:int = 0; i < maxVolts; i++)
			{
				var volt:FlxSprite = new FlxSprite(X + xOffset, Y, ImageFiles.lightningBolt);
				volt.solid = false;
				add(volt, true);
				
				_gaugeVolts.push(volt);
				
				xOffset += volt.width;
			}
			
			_powerText = new FlxText(X - textOffset, Y, 150, "VOLTS:");
			_powerText.setFormat("PowellAntique", 20, 0x778ea1, "left", 0x233e58);
			add(_powerText, true);
		}
		
		override public function update():void
		{
			for (var i:int = 0; i < maxVolts; i++)
			{
				var volt:FlxSprite = _gaugeVolts[i] as FlxSprite;
				volt.visible = false;
			}
			
			if (currentVolts < MINVOLTS)
			{
				currentVolts = MINVOLTS;
			}
			
			if (currentVolts > maxVolts)
			{
				currentVolts = maxVolts;
			}
			
			for (i = 0; i < currentVolts; i++)
			{
				volt = _gaugeVolts[i];
				volt.visible = true;
			}
		}
		
		public function redraw():void
		{
			for (var i:int = 0; i < maxVolts; i++)
			{
				var volt:FlxSprite = _gaugeVolts[i] as FlxSprite;
				if (volt.visible)
				{
					FlxState.screen.draw(volt, volt.x, volt.y);
				}
			}
			
			FlxState.screen.draw(_powerText, _powerText.x, _powerText.y);			
		}
	}

}